package com.d2.plugins.activiti.controller;

import com.d2.plugins.activiti.util.SecurityUtil;
import com.d2.common.domain.Result;
import com.d2.modules.system.permission.util.UserContext;
import lombok.extern.slf4j.Slf4j;
import org.activiti.api.runtime.shared.query.Page;
import org.activiti.api.runtime.shared.query.Pageable;
import org.activiti.api.task.runtime.TaskRuntime;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.zip.ZipInputStream;

@RestController
@RequestMapping("process")
@Slf4j
public class ProcessController {

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private SecurityUtil securityUtil;

    @Autowired
    private TaskRuntime taskRuntime;

    public void deployProcessDefinition(InputStream is, String name, String ext) {
        if (is != null) {
            if (".zip".equals(ext)) {
                ZipInputStream inputStream = new ZipInputStream(is);
                repositoryService.createDeployment()
                        .addZipInputStream(inputStream)
                        .deploy();
            } else if (".bpmn".equals(ext)) {
                repositoryService.createDeployment()
                        .addInputStream(name + ext, is)
                        .deploy();
            }
        }
    }

    @PostMapping("/deploy")
    @ResponseBody
    public Result<Void> deploy() throws FileNotFoundException {
        String name = "myProcess";
        String ext = ".zip";
        FileInputStream fis = new FileInputStream("/Users/mayihua/Downloads/myProcess.zip");
        deployProcessDefinition(fis,name, ext);
        return Result.success();
    }

    @PostMapping("start")
    public Result<Void> startInstance() {
        String username = UserContext.get().getUsername();
        securityUtil.logInAs(username);
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myProcess");
        System.out.println("流程实例ID：" + processInstance.getId());
        return Result.success();
    }

    @GetMapping("tasks")
    public void testTask() {
        securityUtil.logInAs("user1");
        Page<org.activiti.api.task.model.Task> page = taskRuntime.tasks(Pageable.of(0, 10));
        if (page.getTotalItems() > 0) {
            for (org.activiti.api.task.model.Task task : page.getContent()) {
                System.err.println("当前任务有1：" + task);
//                // 拾取
//                taskRuntime.claim(TaskPayloadBuilder.claim().withTaskId(task.getId()).build());
//                // 执行
//                taskRuntime.complete(TaskPayloadBuilder.complete().withTaskId(task.getId()).build());
            }
        } else {
            System.err.println("没的任务1");
        }
    }
}
